package offer.offer40;

import java.util.Arrays;

/**
 * @Author: wjy
 * @Date: 2021/11/3 16:41
 */
public class Solution {

    public static void main(String[] args) {

        int[] ints = {4, 5, 1, 6, 2, 7, 3, 8};

        Solution solution = new Solution();
        int[] leastNumbers = solution.getLeastNumbers(ints, 4);

        for (int i = 0; i < leastNumbers.length; i++) {
            System.out.println(leastNumbers[i]);
        }

    }

    public int[] getLeastNumbers(int[] arr, int k) {
        if (arr.length == 0) {
            return null;
        }

        int[] res = new int[k];
        Arrays.sort(arr);

        for (int i = 0; i < arr.length; i++) {
            if (i < k) {
                res[i] = arr[i];
            }
        }

        return res;
    }
}
